import 'package:flutter/material.dart';
import 'package:jfexpress_ui/layout/single_chart_layout.dart';

import '../../components/axis/oscilloscope_axis.dart';
import '../../components/coord/gauge/oscilloscope_coordinate_system.dart';
import '../core/chart_view.dart';

class OscilloscopeGauge extends StatelessWidget implements ChartView {
  OscilloscopeGauge(this.yAxisList, this.xAxisList, {super.key});

  OscilloscopeCoordinateSystem coordinateSystem =
      const OscilloscopeCoordinateSystem();
  List<OscilloscopeYAxis>? yAxisList;
  List<OscilloscopeXAxis>? xAxisList;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return SingleChartLayout(
          center: coordinateSystem,
          left: buildYAxisList(OscilloscopeYAxisDirection.left),
          right: buildYAxisList(OscilloscopeYAxisDirection.right),
          top: buildXAxisList(OscilloscopeXAxisDirection.top),
          bottom: buildXAxisList(OscilloscopeXAxisDirection.bottom),
        );
      },
    );
  }

  Column buildXAxisList(OscilloscopeXAxisDirection direction) {
    if (xAxisList == null) return const Column();

    List<OscilloscopeXAxis> filteredList =
        xAxisList!.where((r) => r.direction == direction).toList();

    return Column(mainAxisSize: MainAxisSize.max, children: filteredList);
  }

  Row buildYAxisList(OscilloscopeYAxisDirection direction) {
    if (yAxisList == null) return const Row();

    List<OscilloscopeYAxis> filteredList =
        yAxisList!.where((r) => r.direction == direction).toList();

    return Row(mainAxisSize: MainAxisSize.max, children: filteredList);
  }
}
